/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.oai.metadataRepository.harvest.marc21;

import cz.mzk.oai.metadataRepository.metadata.DateStamp;
import java.io.File;

/**
 * This class represents file with exported records (daily updates or primary export) of base mzk03 from aleph with date of it's creation as DateStamp object
 * @author Martin Rehanek, Martin.Rehanek@gmail.com
 */
public class AlephExport implements Comparable {

    private final File file;
    private final DateStamp dateStamp;

    public AlephExport(File file, int datePrefixLength, int dateSuffixLength) {
        this.file = file;
        this.dateStamp = createDateStamp(datePrefixLength,dateSuffixLength);
    }

    private DateStamp createDateStamp(int datePrefixLength,int dateSuffixLength) {
        String fileName = file.getName();
        String year = fileName.substring(datePrefixLength, datePrefixLength +4);
        String month = fileName.substring(datePrefixLength +4 , datePrefixLength + 6);
        String day = fileName.substring(datePrefixLength + 6, datePrefixLength + 8);
        return new DateStamp(year, month, day);
    }

    /**
     * @return the file
     */
    public File getFile() {
        return file;
    }

    /**
     * @return the dateStamp of last change
     */
    public DateStamp getDateStamp() {
        return dateStamp;
    }

    public int compareTo(Object object) {
        if (object == null) {
            throw new NullPointerException();
        }
        if (object instanceof AlephExport) {
            AlephExport ex = (AlephExport) object;
            return dateStamp.compareTo(ex.getDateStamp());
        } else {
            throw new IllegalArgumentException("object must be an instance of AlephDailyUpdateExport");
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof AlephExport)) {
            return false;
        }
        return dateStamp.equals(((AlephExport) obj).getDateStamp());
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.dateStamp != null ? this.dateStamp.hashCode() : 0);
        return hash;
    }
}
